package com.limao.CacheManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.limao.DBDataClass.SysProjectCateDataClass;
import com.limao.DBProvider.CrowdSysCateProvider;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class CrowdSysCateCache extends BasicModule{

	private CrowdSysCateProvider provider;
	
	private static CrowdSysCateCache instance;

    public static CrowdSysCateCache getInstance() {
        return instance;
    }

    private Cache<String, String> SysGoodsCate_CacheManager;

    public CrowdSysCateCache() {
      	super("CrowdSysCateCache");
      	SysGoodsCate_CacheManager = CacheFactory.createCache("da_GoodsCateCache");
        initProvider();
	}
    
    @Override
 	public void initialize() {
         super.initialize();
         instance=this;
     }
 
    public void ClearCacheGoodsCate()
    {
    	SysGoodsCate_CacheManager.remove("CATE_JSON");
    }
    
    
    //&新鲜宝 众筹项目种类	
    public List<SysProjectCateDataClass> loadProjectCateJson()
    {
    	String json=SysGoodsCate_CacheManager.get("CATE_JSON");
		if (CacheClass.isEmpty(json)) 
		{
			ArrayList<SysProjectCateDataClass> list = provider.loadProjectcateAll();
			json=JSON.toJSONString(list);
			if (list.size()>0) 
			{
				SysGoodsCate_CacheManager.put("CATE_JSON", json);
			}
		}
		List<SysProjectCateDataClass> list=new ArrayList<SysProjectCateDataClass>();
		if (!CacheClass.isEmpty(json))
		{
			SysProjectCateDataClass[] nodes = JSON.parseObject(json, new TypeReference<SysProjectCateDataClass[]>(){});    
			list.addAll(Arrays.asList(nodes));
		}
		
        return list;
    }
    
    
    
	private void initProvider() {
		if (provider == null) {
			try {
				provider = CrowdSysCateProvider.getInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
